package com.wunu.smartlock1.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.net.NetComponent;
import com.wunu.smartlock1.net.api.LockEventApi;
import com.wunu.smartlock1.net.api.LockManageApi;
import com.wunu.smartlock1.net.response.ApiNormalResponse;
import com.wunu.smartlock1.net.response.LockEvent;
import com.wunu.smartlock1.net.response.LockQrCode;

import java.util.List;

import retrofit2.Call;

/**
 * Created by admin on 2018/4/21.
 */

public class LockListActivity_net_bak extends AppCompatActivity implements OnClickListener{
    View inflate;
    Dialog dialog;
    Intent intent = new Intent();
    Gson gson = new Gson();
    ImageView addButton;//添加新锁
    ImageView memberButton;//家庭成员
    ImageView temporaryKeyButton;//临时秘钥
    ImageView authorizedButton;//授权成员
    ImageView lockEventButton;//开锁记录
    ImageView lockQRCodeButton;//锁二维码

    TextView cancelView;//取消
    TextView resetView;//重置秘钥
    TextView stopView;//停用蓝牙解锁
    TextView deleteView;//删除门锁
    private Class lzContext;
    private String toast;//弹框提示
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            System.out.println("msg----------------------->:"+msg);
            System.out.println("obj->"+msg.getData());
            if(msg.obj == null){
                Toast.makeText(LockListActivity_net_bak.this,"取"+toast+"失败",Toast.LENGTH_SHORT).show();
                return;
            }
            System.out.println("obj:"+msg.obj);
            Toast.makeText(LockListActivity_net_bak.this,"取"+toast+"成功",Toast.LENGTH_SHORT).show();
            intent.putExtra("param",gson.toJson(msg.obj));
            startActivity(intent.setClass(LockListActivity_net_bak.this, lzContext));
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_lock_list);

        addButton = findViewById(R.id.addButton);
        memberButton = findViewById(R.id.memberButton);
        temporaryKeyButton = findViewById(R.id.temporaryKeyButton);
        authorizedButton = findViewById(R.id.authorizedButton);
        lockEventButton = findViewById(R.id.lockEventButton);
        lockQRCodeButton = findViewById(R.id.lockQRCodeButton);

        //给button按钮绑定单击事件，设置完参数后跳转到首页

        addButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("添加新锁", "list 1:");
                startActivity(intent.setClass(LockListActivity_net_bak.this, ScanListActivity.class));
            }
        });

        memberButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("家庭成员", "list 1:");
                memberShow();
            }
        });
        temporaryKeyButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("临时秘钥", "list 1:");
                startActivity(intent.setClass(LockListActivity_net_bak.this, TemporyLockActivity.class));
            }
        });
        authorizedButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("授权成员", "list 1:");
                startActivity(intent.setClass(LockListActivity_net_bak.this, AuthorizedLockActivity.class));
            }
        });
        lockEventButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                final LockEventApi service = NetComponent.getRetrofit().create(LockEventApi.class);
                Log.v("开锁记录", "list 1:");
                /*startActivity(intent.setClass(LockListActivity.this, LockRecordActivity.class));*/
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            Call<ApiNormalResponse<List<LockEvent>>> call = service.getLockEvents(3, 0, 1, 10);
                            ApiNormalResponse<List<LockEvent>> resp = call.execute().body();
                            // 发送消息，把服务器返回的本文弹出吐司显示
                            Message msg = handler.obtainMessage();
                            if(resp == null || resp.getData() == null)msg.obj = null;
                            else msg.obj = resp.getData().getData();
                            toast = "开锁记录";
                            System.out.println(toast+":"+msg.obj);
                            lzContext = LockRecordActivity.class;
                            handler.sendMessage(msg);
                        } catch (Exception e) {
                            System.out.println("开锁记录异常:" + e);
                            Toast.makeText(LockListActivity_net_bak.this, "取开锁记录异常:" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                };
                thread.start();
            }
        });

        lockQRCodeButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                final LockManageApi service = NetComponent.getRetrofit().create(LockManageApi.class);
                Log.v("锁二维码", "list 1:");
                /*startActivity(intent.setClass(LockListActivity.this, QrcodeActivity.class));*/
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            Call<ApiNormalResponse<LockQrCode>> call = service.getLockQrCode(3);
                            ApiNormalResponse<LockQrCode> resp = call.execute().body();
                            System.out.println(resp.toString());
                            // 发送消息，把服务器返回的本文弹出吐司显示
                            Message msg = handler.obtainMessage();
                            if(resp == null || resp.getData() == null)msg.obj = null;
                            else msg.obj = resp.getData().getData();
                            toast = "锁二维码";
                            lzContext = QrcodeActivity.class;
                            handler.sendMessage(msg);
                        } catch (Exception e) {
                            System.out.println("二维码异常:" + e);
                            Toast.makeText(LockListActivity_net_bak.this, "取二维码异常:" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                };
                thread.start();
            }
        });
    }
    public void memberShow(){
        dialog = new Dialog(this,R.style.smartlock_ActionSheetDialogStyle);
        inflate = LayoutInflater.from(this).inflate(R.layout.smartlock_buttom_dialog, null);


        cancelView = inflate.findViewById(R.id.cancel);//取消弹框
        resetView = inflate.findViewById(R.id.resetlock);//取消弹框
        deleteView = inflate.findViewById(R.id.deletelock);//取消弹框
        stopView = inflate.findViewById(R.id.stoplock);//取消弹框
        resetView.setOnClickListener(this);
        cancelView.setOnClickListener(this);
        deleteView.setOnClickListener(this);
        stopView.setOnClickListener(this);
        dialog.setContentView(inflate);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity( Gravity.BOTTOM);
       /* WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 20;
        dialogWindow.setAttributes(lp);*/
        dialog.show();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.cancel){
            dialog.dismiss();
        }else if(id == R.id.resetlock){
            Toast.makeText(this,"点击了重置秘钥按钮",Toast.LENGTH_SHORT).show();
        }else if(id == R.id.deletelock){
            Toast.makeText(this,"点击了删除门锁按钮",Toast.LENGTH_SHORT).show();
        }else if(id == R.id.stoplock){
            Toast.makeText(this,"点击了停用蓝牙解锁按钮",Toast.LENGTH_SHORT).show();
        }
        //dialog.dismiss();
    }
}